package eerbaar.client.internal.ctr.operator.area;

public class Operator {

    //@formatter:off
    private static double[] opLongRange = new double[] {
            10,
            9,
            8,
            7,
            6,
            5,
            4,
            3,
            2,
            1,
            0.5,
            0.2,
            0
    };
    //@formatter:on

    //@formatter:off
    private static double[] opShortRange = new double[] {
        10,
        9,
        8,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
    };
    //@formatter:on

    private static double extract(double distance, double[] array) {

        int floor = (int) distance;
        int ceil = floor + 1;

        double d1 = array[floor];
        if (ceil > 12) {
            return d1;
        }

        double d2 = array[ceil];

        double dd = 0;
        if (floor > 0) {
            dd = (((distance - floor) * (d2 - d1)) / floor);
        }
        return dd + d1;
    }

    private static double op(double distance, int gradient, double d) {
        return (d * gradient);
    }

    public static double opAdjustableRange(double distance, int gradient) {

        if (distance > Math.abs(gradient)) {
            return 0;
        }
        return opLongRange(distance, gradient);
    }

    public static double opLongRange(double distance, int gradient) {

        double d = extract(distance, opLongRange);
        return op(distance, gradient, d);
    }

    public static double opPositiveAndNegative(double distance, int gradient) {

        int newGradient = gradient - 5;
        if (distance > Math.abs(newGradient)) {
            return 0;
        }
        double sign = ((newGradient > 0) ? 1 : -1);
        double d = extract(distance, opLongRange);
        double result = op(distance, Math.abs(newGradient), d);
        return (result * sign);
    }

    public static double opShortRange(double distance, int gradient) {

        double d = extract(distance, opShortRange);
        return op(distance, gradient, d);
    }

}
